<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Order;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Order\Ctl\PayTplCtl;
use Srv\Apps\Proto\DTO\Order\PayTplAddDTO;
use Srv\Apps\Proto\DTO\Order\PayTplEditDTO;
use Srv\Apps\Proto\DTO\Order\PayTplListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Order\PayTplListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class PayTplAct extends ActionAbstract
{
    /**
     * @url /pay/tpl/list
     * @auth payTplList
     * @req Srv.Apps.Proto.DTO.Order.PayTplListDTO
     * @rsp Srv.Apps.Proto.VO.Order.PayTplListVO
     * 付费模板列表
     */
    public function getPayTplList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof PayTplListDTO) || !($VOClass instanceof PayTplListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = PayTplCtl::tag()->getPayTplList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /pay/tpl/add
     * @auth payTplAdd
     * @req Srv.Apps.Proto.DTO.Order.PayTplAddDTO
     * 付费模板新增
     */
    public function payTplAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof PayTplAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        $pgspId     = PayTplCtl::tag()->payTplAdd($setData);
        if($pgspId < 1) {
            $errMsg = '付费模板新增失败';
            $LogCtl->logAdd(LogCtl::PAY_TPL_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = '付费模板新增成功';
        $LogCtl->logAdd(LogCtl::PAY_TPL_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /pay/tpl/edit
     * @auth payTplEdit
     * @req Srv.Apps.Proto.DTO.Order.PayTplEditDTO
     * 付费模板编辑
     */
    public function payTplEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof PayTplEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $tppdId         = intval($DTOClass->getTppdId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $PayTplCtl      = PayTplCtl::tag();
        $payTplInfoSrc  = $PayTplCtl->getPayTplSrc($tppdId);
        $logData        = $setData;
        if(!isset($payTplInfoSrc['tppd_id'])){
            $errMsg     = '付费模板不存在';
            $LogCtl->logAdd(LogCtl::PAY_TPL_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $platformOld    = intval($payTplInfoSrc['tppd_platform']);
        $firstOld       = intval($payTplInfoSrc['tppd_first']);
        $result         = $PayTplCtl->payTplEdit($tppdId, $setData, $platformOld, $firstOld);
        $logData        = ['new' => $setData, 'old' => $payTplInfoSrc];
        if(!$result) {
            $errMsg     = '付费模板编辑失败';
            $LogCtl->logAdd(LogCtl::PAY_TPL_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '付费模板编辑成功';
        $LogCtl->logAdd(LogCtl::PAY_TPL_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}